package com.creditease.mobile.news.services;

import android.util.Log;
import com.creditease.mobile.news.db.DBUtils;
import com.creditease.mobile.news.db.NewsSQLiteOpenHelper;
import com.creditease.mobile.news.model.NewsModel;
import com.creditease.mobile.rss.RssConst;
import com.creditease.mobile.util.FileUtil;
import com.creditease.mobile.util.HttpUtils;
import com.creditease.mobile.util.PicDownloadUtil;
import com.creditease.mobile.util.StreamUtil;
import com.creditease.mobile.vo.NewsDetail;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with creditease.
 * User: lirong
 * Date: 12-5-22
 * Time: 上午8:15
 */
public class NewsServices {
    private String TAG = NewsServices.class.getName();

    public NewsDetail getRemoteNewsDetail(String cid, String docid, NewsSQLiteOpenHelper dbHelper,boolean downloadImg) {

        NewsDetail newsDetail = null;
        String url = ("http://c.3g.163.com/nc/article/" + docid + "/" + "head" + ".html");
        InputStream is;
        try {
            is = HttpUtils.getHttpStream(url);

            String body = null;
            String source = null;
            String ptime = null;
            String title = null;
            if (is != null) {
                String str = StreamUtil.stream2String(is);
                Log.d(TAG, "json=" + str);
                JSONObject json = new JSONObject(str);
                JSONObject array = json.getJSONObject(docid);
                Log.d(TAG, "object=" + array);
                body = array.getString("body");
                title = array.getString("title");
                source = array.getString("source");
                String hasNext = array.getString("hasNext");
                ptime = array.getString("ptime");

                JSONArray jarray = array.getJSONArray("img");
                Log.d(TAG, "hasNext=" + hasNext);
                Log.d(TAG, "img=" + jarray);

                for (int i = 0; i < jarray.length(); i++) {

                    JSONObject obj = jarray.optJSONObject(i);
                    String src = obj.getString("src");
//                    imgs.add(src);
                    String path = FileUtil.getFileName(src);
                    String filePath = RssConst.NEWS_IMG_CACHE + path;
                    if(downloadImg){
                        PicDownloadUtil.add2Queue(src, filePath);
                    }
                    dbHelper.insertDataToImgTable(cid, docid, src, filePath);
                }

                newsDetail = new NewsDetail(null, docid, title, ptime, body,
                        hasNext, source);
                dbHelper.insertDataToNewsTable(cid, docid, newsDetail.body,
                        newsDetail.title, newsDetail.hasNext, newsDetail.ptime,
                        newsDetail.source);
                return newsDetail;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return newsDetail;
    }

    public String getRemoteNewsDetailTail(String docid, NewsSQLiteOpenHelper dbHelper) throws IOException {

        NewsDetail newsDetail = null;
        String url = ("http://c.3g.163.com/nc/article/" + docid + "/" + "tail.html");
        InputStream is;
        try {
            is = HttpUtils.getHttpStream(url);
            String body = null;
            newsDetail = DBUtils.getDataFromNewsTable(dbHelper, docid);
            if (is != null) {
                String str = StreamUtil.stream2String(is);
                Log.d(TAG, "json=" + str);
                JSONObject json = new JSONObject(str);
                JSONObject array = json.getJSONObject(docid);
                Log.d(TAG, "object=" + array);
                body = array.getString("body");
                newsDetail.body = newsDetail.body + body;
                newsDetail.hasNext = "false";
                dbHelper.updateNewsDB(newsDetail);
                return body;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        return "";
    }

    public List<NewsModel> getRemoteData(String tid, int start, int max, NewsSQLiteOpenHelper dbHelper) throws IOException {

        String url = "http://c.3g.163.com/nc/article/headline/" + tid + "/" + start + "-" + max + ".html";
        try {
            InputStream is = HttpUtils.getHttpStream(url);
            if (is != null) {
                String str = StreamUtil.stream2String(is);
                //清空数据
//                DBUtils.deleteDataToNewsListTabel(dbHelper.getWritableDatabase(), tid);
                if (start == 0) {
                    dbHelper.deleteDataToNewsListTabel(tid);
                }
                Log.d(TAG, "json=" + str);
                JSONObject json = new JSONObject(str);
                JSONArray array = json.getJSONArray(tid);
                Log.d(TAG, "object=" + array);

                Log.d(TAG, "delete cid=" + tid);
                List<NewsModel> list = new ArrayList<NewsModel>();
                for (int i = 0; i < array.length(); i++) {
                    JSONObject obj = array.getJSONObject(i);
                    NewsModel news = new NewsModel();
                    news.cid = tid;

                    news.url_3w = obj.getString("url_3w");
                    news.replyCount = obj.getString("replyCount");
                    if (obj.has("hasImg")) {
                        news.hasImg = obj.getString("hasImg");
                    }
                    news.digest = obj.getString("digest");
                    news.url = obj.getString("url");
                    news.title = obj.getString("title");
                    news.docid = obj.getString("docid");

                    if (obj.has("order")) {
                        news.order = obj.getString("order");
                    }
                    if (obj.has("priority")) {
                        news.priority = obj.getString("priority");
                    }
                    news.lmodify = obj.getString("lmodify");
                    news.subtitle = obj.getString("subtitle");
                    news.imgsrc = obj.getString("imgsrc");
                    news.ptime = obj.getString("ptime");
                    Log.d(TAG, news.toString());
                    list.add(news);
                }

                return list;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        }
        return new ArrayList<NewsModel>();
    }
}
